<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
            :confirmButtonText="false"
        >
<!--            <div v-for="(item, index) in messageJsonArr" :key="index">-->
<!--                <h2 v-if="item.value">{{item.flag}}: {{item.value}}</h2>-->
<!--                <h2 v-else>{{item.flag}}: {{item.switcher}}</h2>-->
<!--            </div>-->

            <el-input type="textarea"
                      autosize
                      placeholder="请输入内容"
                      v-model="formData.messageJson"></el-input>

        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { deviceMessageDetail } from '@/api/device_test'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return "消息详情"
})

const formData = reactive({
    id: '',
    deviceId: '',
    address: '',
    clientId: '',
    messageJson: '',
})

const messageJsonPretty = computed(() => {
    if (formData.messageJson !== undefined && formData.messageJson !== '') {
        let a = JSON.parse(formData.messageJson);
        return JSON.stringify(a, null, 4);
    } else {
        return '';
    }
})

const messageJsonArr = computed(() => {
    if (formData.messageJson !== undefined && formData.messageJson !== '') {
        console.log("******", formData.messageJson)
        let a = JSON.parse(formData.messageJson);
        console.log("-----", a)
        return a;
    } else {
        return [];
    }
})


const formRules = {
    id: [
        {
            required: true,
            message: '请输入主键id',
            trigger: ['blur']
        }
    ],
    deviceId: [
        {
            required: true,
            message: '请输入设备id',
            trigger: ['blur']
        }
    ],
    address: [
        {
            required: true,
            message: '请输入设备mac地址',
            trigger: ['blur']
        }
    ],
    clientId: [
        {
            required: true,
            message: '请输入设备客户端名称',
            trigger: ['blur']
        }
    ],
    messageJson: [
        {
            required: true,
            message: '请输入消息内容',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await messageEdit(data) : await messageAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    console.log(row);
    // const data = await deviceMessageDetail({
    //     id: row.id
    // })
    setFormData(row)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
